package com.zn.web.controller;

import com.zn.entity.*;
import com.zn.service.AppBannerService;
import com.zn.service.AppIconService;
import com.zn.service.AppPopupWindowService;
import com.zn.vo.Message;
import com.zn.web.dto.AppCustomerDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/appHomePage")
public class PageController {

    private static final Logger LOGGER = LoggerFactory.getLogger(PageController.class);

    @Autowired
    private AppBannerService appBannerService;

    @Autowired
    private AppIconService appIconService;

    @Autowired
    private AppPopupWindowService appPopupWindowService;

    /**
     * 获取banner列表
     * @author chenwenjing
     * @time 2019/1/3 13:03
     * @param
     */
    @RequestMapping(value = "/getAppBannerList",method = RequestMethod.POST)
    public ResponseEntity<Message> getAppBannerList(){
        try{
            List<AppBanner> appBannerList=appBannerService.findAll();
            return ResponseEntity.ok(Message.successData(appBannerList));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 获取icon列表
     * @author chenwenjing
     * @time 2019/1/3 13:04
     * @param
     */
    @RequestMapping(value = "/getAppIconList",method = RequestMethod.POST)
    public ResponseEntity<Message> getAppIconList(){
        try{
            List<AppIcon> appIconList=appIconService.findAll();
            return ResponseEntity.ok(Message.successData(appIconList));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 获取有效的文章类型菜单
     * @return
     */
    @RequestMapping(value = "/getAppInformationTypeEffective",method = RequestMethod.POST)
    public ResponseEntity<Message> getAppInformationTypeEffective() {
        try {
            List<AppInformationType> AppInformationTypeList = appBannerService.findAppInformationTypeEffective();
            return ResponseEntity.ok(Message.successData(AppInformationTypeList));
        }catch (Exception ex){
            LOGGER.error("getAppInformationTypeEffective........."+ex.getMessage());
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 分页查询有效的文章和其它快讯信息
     * @param record
     * @return
     */
    @RequestMapping(value = "/findAppInformationEffectiveList",method = RequestMethod.POST)
    public ResponseEntity<Message> findAppInformationEffectiveList(@RequestBody AppCustomerDto record) {
        try {
            if (StringUtils.isBlank(record.getInformationTypeId())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            LOGGER.info("findAppInformationEffectiveList.............................."+record.toString());
            List<?> list = new ArrayList<>();
            if ("1".equals(record.getInformationSource())) {
                list = appBannerService.findAppInformationEffective(record);
            }else {
                list = appBannerService.findJinshiDataList(record);
            }
            return ResponseEntity.ok(Message.page(list));
        }catch (Exception ex){
            LOGGER.error("findAppInformationEffectiveList........."+ex.getMessage());
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 分页查询72快讯信息
     * @param record
     * @return
     */
    @RequestMapping(value = "/findJinshiDataList",method = RequestMethod.POST)
    public ResponseEntity<Message> findJinshiDataList(@RequestBody AppCustomerDto record) {
        try {
            LOGGER.info("findJinshiDataList.............................."+record.toString());
            List<JinshiData> JinshiDataList = appBannerService.findJinshiDataList(record);
            return ResponseEntity.ok(Message.page(JinshiDataList));
        }catch (Exception ex){
            LOGGER.error("findJinshiDataList........."+ex.getMessage());
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 根据code查询icon
     * @author chenwenjing
     * @time 2019/1/8 18:38
     * @param
     */
    @RequestMapping(value = "/findAppIconByCode",method = RequestMethod.POST)
    public ResponseEntity<Message> findAppIconByCode(@RequestBody AppCustomerDto record){
        try {
            if (StringUtils.isBlank(record.getCode())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            List<AppIcon> list= appIconService.findAppIconByCode(record);
            return ResponseEntity.ok(Message.successData(list));
        }catch (Exception ex){
            LOGGER.error("findAppIconByCode........."+ex.getMessage());
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 获取弹窗列表
     * @author chenwenjing
     * @time 2019/2/25 13:31
     * @param
     */
    @RequestMapping(value = "/getAppPopupWindowList",method = RequestMethod.POST)
    public ResponseEntity<Message> getAppPopupWindowList(){
        try{
            List<AppPopupWindow> appPopupWindowList=appPopupWindowService.findAll();
            return ResponseEntity.ok(Message.successData(appPopupWindowList));
        }catch (Exception e){
            LOGGER.error("getAppPopupWindowList........."+e.getMessage());
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 定时每天05：00：00清除5天前的所有金十数据
     * @author chenwenjing
     * @time 2019/2/28 14:44
     * @param
     */
//    @Scheduled(cron="0 00 05 * * ?")
//    public void delectJinShiData() throws Exception{
//        int count = appBannerService.findDay();
//        if (count != 0){
//            appBannerService.delectJinShiData();
//        }
//    }
}
